package com.offcn.utils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;

public class BaseServlet  extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //需要知道访问的是哪个Servlet
        Class aClass = this.getClass();
         //req.setCharacterEncoding("utf-8");
         //resp.setContentType("text/html;charset=utf-8");
         //获取方法名
        String method = req.getParameter("method");

        try {
            //通过方法名获取Method对象
            Method m = aClass.getMethod(method, HttpServletRequest.class, HttpServletResponse.class);
            //通过对象调用方法
            m.invoke(this,req,resp);
        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}
